﻿using Sidvall.Globalization;
using Sidvall.Serialization;

namespace Sidvall.UI.Wpf.Globalization
{
    public class CultureProvider : ICultureProvider
    {
        #region Public Members

        #region EnsureCorrectCurrentUICulture

        public void EnsureCorrectCurrentUICulture()
        {
            if (System.Globalization.CultureInfo.DefaultThreadCurrentUICulture == null)
                return;
            if (!System.Globalization.CultureInfo.CurrentUICulture.Name.Equals(System.Globalization.CultureInfo.DefaultThreadCurrentUICulture.Name, true, System.StringComparison.OrdinalIgnoreCase))
            {
                var cultureInfo = new System.Globalization.CultureInfo(System.Globalization.CultureInfo.DefaultThreadCurrentUICulture.Name);
                SetCurrentUICulture(cultureInfo);
            }
        }

        #endregion
        #region SetCurrentCulture

        public void SetCurrentCulture(System.Globalization.CultureInfo cultureInfo)
        {
            System.Globalization.CultureInfo.CurrentCulture = cultureInfo;
            System.Globalization.CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
            Sidvall.UI.Wpf.ControlManager.SetLanguage(Sidvall.UI.Wpf.SystemContext.Current.MainWindow, cultureInfo);
        }

        #endregion
        #region SetCurrentUICulture

        public void SetCurrentUICulture(System.Globalization.CultureInfo cultureInfo)
        {
            System.Globalization.CultureInfo.CurrentUICulture = cultureInfo;
            if ((System.Globalization.CultureInfo.DefaultThreadCurrentUICulture == null) ||
                (!System.Globalization.CultureInfo.DefaultThreadCurrentUICulture.Name.Equals(cultureInfo.Name, true, System.StringComparison.OrdinalIgnoreCase)))
            {
                System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
                Sidvall.UI.Messaging.CultureMessage.Publish(this);
            }
        }

        #endregion

        #endregion
    }
}
